package org.tanjor.common.shardingjdbc.shardrule.table;

import java.util.Collection;
import java.util.LinkedHashSet;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import com.google.common.collect.Range;

public class SingleTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<String> {

	@Override
	public String doEqualSharding(Collection<String> tableNames, ShardingValue<String> shardingValue) {
		// TODO Auto-generated method stub
		for (String each : tableNames) {
			return each;
		}
		throw new IllegalArgumentException();
	}

	@Override
	public Collection<String> doInSharding(Collection<String> tableNames, ShardingValue<String> shardingValue) {
		// TODO Auto-generated method stub
		Collection<String> result = new LinkedHashSet<>(tableNames.size());
		for(int i=0;i<shardingValue.getValues().size();i++){
			for (String tableName : tableNames) {
				result.add(tableName);
			}
		}
		return result;
	}

	@Override
	public Collection<String> doBetweenSharding(Collection<String> tableNames, ShardingValue<String> shardingValue) {
		// TODO Auto-generated method stub
		Collection<String> result = new LinkedHashSet<>(tableNames.size());
		Range<String> range = (Range<String>) shardingValue.getValueRange();
		for (Long i = Long.valueOf(range.lowerEndpoint()); i <= Long.valueOf(range.upperEndpoint()); i++) {
			for (String each : tableNames) {
				result.add(each);
			}
		}
		return result;
	}
}
